package com.hzw.code.utils

import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import java.io.Serializable

/**
 *
 * @Author:         HuaZhongWei
 * @CreateDate:     2020/4/15 10:51
 *
 * @Description:    Intent扩展工具
 *
 */

fun newIntentWithArg(params: Array<out Pair<String, Any?>>): Intent {
    if (params.isNullOrEmpty()) {
        return Intent()
    }
    return Intent().apply {
        params.forEach { params ->
            when (val value = params.second) {
                null -> putExtra(params.first, null as Serializable?)
                is Int -> putExtra(params.first, value)
                is Long -> putExtra(params.first, value)
                is CharSequence -> putExtra(params.first, value)
                is String -> putExtra(params.first, value)
                is Float -> putExtra(params.first, value)
                is Double -> putExtra(params.first, value)
                is Char -> putExtra(params.first, value)
                is Short -> putExtra(params.first, value)
                is Boolean -> putExtra(params.first, value)
                is Serializable -> putExtra(params.first, value)
                is Bundle -> putExtra(params.first, value)
                is Parcelable -> putExtra(params.first, value)
                is Array<*> -> when {
                    value.isArrayOf<CharSequence>() -> putExtra(params.first, value)
                    value.isArrayOf<String>() -> putExtra(params.first, value)
                    value.isArrayOf<Parcelable>() -> putExtra(params.first, value)
                    else -> throw Exception("Intent extra ${params.first} has wrong type ${value.javaClass.name}")
                }
                is IntArray -> putExtra(params.first, value)
                is LongArray -> putExtra(params.first, value)
                is FloatArray -> putExtra(params.first, value)
                is DoubleArray -> putExtra(params.first, value)
                is CharArray -> putExtra(params.first, value)
                is ShortArray -> putExtra(params.first, value)
                is BooleanArray -> putExtra(params.first, value)
                else -> throw Exception("Intent extra ${params.first} has wrong type ${value.javaClass.name}")
            }
        }
    }
}